/*
 * ContribuyentesImpuestosVerFrame.java
 *
 * Created on 24 de abril de 2008, 06:49 PM
 */
package org.contabilidad.cierres;

import org.mybeans.Sesion;
import org.mybeans.CierreMensualBeans;
import java.awt.Cursor;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

/**
 *
 * @author  avbravo
 */
public class CierreMensualCrearFrame extends javax.swing.JFrame {

    CierreMensualBeans cierreMensualBeans;
    CierreMensualContableContribuyentes cierreMensualContableContribuyentes;
    CierreMensualContableComercios cierreMensualContableComercios;
    CierreMensualContableVehiculos cierreMensualContableVehiculos;
    CierreMensualContableRemolques cierreMensualContableRemolques;
    CierreMensualContableVigenciasExpiradas cierreMensualContableVigenciasExpiradas;
    Sesion sesion;
    String Mes;
    int Anio;
    int fila = -1;

    /** Creates new form ContribuyentesImpuestosVerFrame */
    public CierreMensualCrearFrame() {
        initComponents();
        sesion = new Sesion();

        /*
         * Fondo
         */

         
         
         

        jPanelFondo.setOpaque(false);
        setContentPane(jPanelFondo);
        /*
         * fondo
         */


        cierreMensualBeans = new CierreMensualBeans();
        cierreMensualContableContribuyentes = new CierreMensualContableContribuyentes();
        cierreMensualContableComercios = new CierreMensualContableComercios();
        cierreMensualContableRemolques = new CierreMensualContableRemolques();
        cierreMensualContableVehiculos = new CierreMensualContableVehiculos();
        cierreMensualContableVigenciasExpiradas = new CierreMensualContableVigenciasExpiradas();
        java.util.Calendar ca = java.util.Calendar.getInstance();
        java.sql.Date mydate = new java.sql.Date(ca.getTimeInMillis());
        jFormattedTextFieldFecha.setValue(mydate);
        int year = ca.get(java.util.Calendar.YEAR);
        jTextFieldAnio.setText(String.valueOf(year));
        this.Anio = year;
        CargarComboMesesSinCerrar();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDialog1 = new javax.swing.JDialog();
        jPanelFondo = new javax.swing.JPanel();
        jButtonProcesarCierre = new javax.swing.JButton();
        jButtonRegresar = new javax.swing.JButton();
        jButtonVerMesesCerrados = new javax.swing.JButton();
        jLabel14 = new javax.swing.JLabel();
        jTextFieldAnio = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        jComboBoxMesPago = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jFormattedTextFieldFecha = new javax.swing.JFormattedTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();

        javax.swing.GroupLayout jDialog1Layout = new javax.swing.GroupLayout(jDialog1.getContentPane());
        jDialog1.getContentPane().setLayout(jDialog1Layout);
        jDialog1Layout.setHorizontalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jDialog1Layout.setVerticalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cierre Mensual");

        jPanelFondo.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jButtonProcesarCierre.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/tools.png"))); // NOI18N
        jButtonProcesarCierre.setToolTipText("Procesar cierre mensual");
        jButtonProcesarCierre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonProcesarCierreActionPerformed(evt);
            }
        });

        jButtonRegresar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/regresar.jpeg"))); // NOI18N
        jButtonRegresar.setToolTipText("Regresar");
        jButtonRegresar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRegresarActionPerformed(evt);
            }
        });

        jButtonVerMesesCerrados.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/view.png"))); // NOI18N
        jButtonVerMesesCerrados.setToolTipText("Ver Meses Cerrados");
        jButtonVerMesesCerrados.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonVerMesesCerradosActionPerformed(evt);
            }
        });

        jLabel14.setText("Año");

        jLabel12.setText("Mes a Cerrar");

        jComboBoxMesPago.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre" }));
        jComboBoxMesPago.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxMesPagoActionPerformed(evt);
            }
        });

        jLabel1.setText("Fecha");

        jFormattedTextFieldFecha.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("dd/MM/yyyy"))));
        jFormattedTextFieldFecha.setEnabled(false);

        jLabel2.setText("Nota:");

        jLabel3.setText("Este proceso solo debe ser ejecutado al finalizar el mes.");

        jLabel4.setText("No se recomienda que realice otras actividades hasta que finalize este proceso.");

        javax.swing.GroupLayout jPanelFondoLayout = new javax.swing.GroupLayout(jPanelFondo);
        jPanelFondo.setLayout(jPanelFondoLayout);
        jPanelFondoLayout.setHorizontalGroup(
            jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelFondoLayout.createSequentialGroup()
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelFondoLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel12)
                            .addComponent(jLabel14))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanelFondoLayout.createSequentialGroup()
                                .addComponent(jTextFieldAnio, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(106, 106, 106)
                                .addComponent(jLabel1)
                                .addGap(18, 18, 18)
                                .addComponent(jFormattedTextFieldFecha, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jComboBoxMesPago, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanelFondoLayout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 531, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3)))
                    .addGroup(jPanelFondoLayout.createSequentialGroup()
                        .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanelFondoLayout.createSequentialGroup()
                                .addGap(54, 54, 54)
                                .addComponent(jButtonProcesarCierre, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel2))
                        .addGap(18, 18, 18)
                        .addComponent(jButtonVerMesesCerrados, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(22, 22, 22)
                        .addComponent(jButtonRegresar, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(85, 85, 85))
        );
        jPanelFondoLayout.setVerticalGroup(
            jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelFondoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelFondoLayout.createSequentialGroup()
                        .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel14)
                            .addGroup(jPanelFondoLayout.createSequentialGroup()
                                .addComponent(jTextFieldAnio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                        .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanelFondoLayout.createSequentialGroup()
                                .addGap(2, 2, 2)
                                .addComponent(jLabel12))
                            .addGroup(jPanelFondoLayout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jComboBoxMesPago, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(jFormattedTextFieldFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(28, 28, 28)
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jButtonProcesarCierre, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButtonVerMesesCerrados, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jButtonRegresar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel3)
                .addGap(18, 18, 18)
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(37, 37, 37))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelFondo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelFondo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButtonRegresarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRegresarActionPerformed

    dispose();
}//GEN-LAST:event_jButtonRegresarActionPerformed

private void jButtonProcesarCierreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonProcesarCierreActionPerformed

    try {
        String SAnio = jTextFieldAnio.getText();

        if (SAnio == null || SAnio.equals("")) {
            JOptionPane.showMessageDialog(this, "No ha seleccionado el año ", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            jTextFieldAnio.requestFocus();
            return;
        }
        this.Anio = Integer.parseInt(SAnio);
        /*
         * Se le suma uno al año para que genere los 
         * impuestos para el siguiente año
         */

        if (this.Anio <= 2007) {
            JOptionPane.showMessageDialog(this, "Debe ingresar un año valido ", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            jTextFieldAnio.requestFocus();
            return;
        }
        this.Mes = (String) this.jComboBoxMesPago.getSelectedItem();

        if (this.Mes == null || this.Mes.equals("")) {
            JOptionPane.showMessageDialog(this, "seleccione el mes de pago", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        if (cierreMensualBeans.VerificarExiste(this.Anio, this.Mes) == true) {
            JOptionPane.showMessageDialog(null, "Ya se creo un cierre mensual para ese mes.", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        cierreMensualBeans.setAnio(this.Anio);
        cierreMensualBeans.setMes(this.Mes);
        java.util.Date myFecha = (java.util.Date) jFormattedTextFieldFecha.getValue();
        java.sql.Date date = new java.sql.Date(myFecha.getTime());
        cierreMensualBeans.setFecha(date);


        /*
         * Contribuyentes
         *
         */


        cierreMensualContableContribuyentes.setAnio(Anio);
        cierreMensualContableContribuyentes.setFecha(date);
        cierreMensualContableContribuyentes.setMes(Mes);
        cierreMensualContableContribuyentes.IniciarCierre();

        /*
         * Comercios
         */

        cierreMensualContableComercios.setAnio(Anio);
        cierreMensualContableComercios.setFecha(date);
        cierreMensualContableComercios.setMes(Mes);
        cierreMensualContableComercios.IniciarCierre();
        /*
         * Vehiculos
         */
        cierreMensualContableVehiculos.setAnio(Anio);
        cierreMensualContableVehiculos.setFecha(date);
        cierreMensualContableVehiculos.setMes(Mes);
        cierreMensualContableVehiculos.IniciarCierre();

        /*
         * Remolques
         */
        cierreMensualContableRemolques.setAnio(Anio);
        cierreMensualContableRemolques.setFecha(date);
        cierreMensualContableRemolques.setMes(Mes);
        cierreMensualContableRemolques.IniciarCierre();
        /*
         * 
         * Procesar Vigencias expiradas
         */

        cierreMensualContableVigenciasExpiradas.setAnio(this.Anio);
        cierreMensualContableVigenciasExpiradas.setMes(Mes);
        cierreMensualContableVigenciasExpiradas.ProcesarMorosidadMensualVigencias();

        setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
        cierreMensualBeans.Agregar();
        jButtonProcesarCierre.setEnabled(false);
        setCursor(null);
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
    }
}//GEN-LAST:event_jButtonProcesarCierreActionPerformed

private void jButtonVerMesesCerradosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonVerMesesCerradosActionPerformed

    CierreMensualVerMesesCerradosFrame cierreMensualVerFrame = new CierreMensualVerMesesCerradosFrame();
    cierreMensualVerFrame.setLocationRelativeTo(null);
    cierreMensualVerFrame.setVisible(true);
}//GEN-LAST:event_jButtonVerMesesCerradosActionPerformed

private void jComboBoxMesPagoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxMesPagoActionPerformed
    // TODO add your handling code here:
}//GEN-LAST:event_jComboBoxMesPagoActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new CierreMensualCrearFrame().setVisible(true);
            }
        });
    }

    /*
     * 
     * Procesa los contribuyentes
     * 
     */
    /*
     * 
     * Procesa los comercios
     * 
     */
    private void Limpiar() {
    }

    private void CargarComboMesesSinCerrar() {

        try {


            if (sesion.Conectar() == false) {
                JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            int count = 0;
            String[] arrayMeses = sesion.getArrayMeses();
            jComboBoxMesPago.removeAllItems();
            for (int i = 0; i <= 11; i++) {
                String mes = arrayMeses[i];
                String sql = "select cierremensual.* from  cierremensual  where idmunicipio=? and anio = ? and mes = ? and activo='si' order by fecha";
                PreparedStatement psquery = sesion.con.prepareStatement(sql);
                psquery.setObject(1, sesion.getIdmunicipio());
                psquery.setInt(2, this.Anio);
                psquery.setString(3, mes);
                ResultSet rs = psquery.executeQuery();
                rs.next();
                int x = rs.getRow();
                if (x == 0) {
                    jComboBoxMesPago.insertItemAt(mes, count);
                    count++;
                    //  break;
                }

            }

            if (count != 0) {
                jComboBoxMesPago.setSelectedItem(sesion.getMunicipio());
            }// Hay tres columnas en la tabla
            else {
                JOptionPane.showMessageDialog(this, "Todos los meses de este año se cerraron", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                jButtonProcesarCierre.setEnabled(false);
            }

            sesion.con.close();

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "CargarComboMesesSinCerrar()\nError " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonProcesarCierre;
    private javax.swing.JButton jButtonRegresar;
    private javax.swing.JButton jButtonVerMesesCerrados;
    private javax.swing.JComboBox jComboBoxMesPago;
    private javax.swing.JDialog jDialog1;
    private javax.swing.JFormattedTextField jFormattedTextFieldFecha;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanelFondo;
    private javax.swing.JTextField jTextFieldAnio;
    // End of variables declaration//GEN-END:variables
}
